import { defineStore } from 'pinia'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'
import $ajax from '../api/to8081.js'
import $ajaxGrid from '../api/to8082.js'
import { alertCode } from '@/utils/msg.js'

export const useGridors = defineStore('gridors', {
  state: () => ({
    /** @type {{ id:number, rname:String, nbl:number, remark:String }[]} */
    data: [],
    curRow:'',
    curPage:'',
    pageSize:'',
    editVisiable: false,
    options:[],
    states:[
       {
      value: 0,
      label: '正常'
       },
      {
        value: 1,
        label: '休假'
      },
      {
        value: 2,
        label: '其他'
      },
    ],
    curState:'',

  }),
  getters: {



  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    async loadData() {
      if (this.data === null || this.data.length === 0) {
        const path = '/member/gridor/list'
        const res = await $ajax.get(path)
        if (res.code === 200) {
          this.data = res.data
          console.log(this.data)
        }
      }
    },
    async loadPCR(){
      const path = 'grid/list'
      const res = await $ajaxGrid.get(path)
      if(res.code === 200) {
        this.options = res.data
        console.log(this.options)
      }
    },
    edit(row) {
      this.curRow = row
      this.curState = this.states[row.state].value
      console.log(row)
    },
    openEdit(){
      this.editVisiable = true;
    },
    closeEdit(){
      console.log(this.curState)
      this.editVisiable = false;
    },
    editConfirm() {
      this.updateAddress();
      this.editCommit()
      this.closeEdit()
    },
    async editCommit() {
      this.curRow.state = this.curState
      const path = '/member/gridor/edit'
      const res = await $ajax.post(path,this.curRow)

      if (res.code === 200) {
        console.log('修改成功')
      }
    },
    async updateAddress() {
      const path = '/grid/get/' + this.curRow.gids[this.curRow.gids.length - 1]
      const res = await $ajaxGrid.get(path)
      if (res.code === 200) {
        console.log(this.curRow)
        this.curRow.address = res.data.content
        console.log('地址更新成功')
      }
    }
  }

})
